package upyun

import (
	"dc-news/common/global/setting"
	"fmt"
	"github.com/upyun/go-sdk/v3/upyun"
	"go.uber.org/zap"
)

var up *upyun.UpYun

func InitUpYun() (err error) {
	up = upyun.NewUpYun(&upyun.UpYunConfig{
		Bucket:   setting.Conf.UpYunConfig.Bucket,
		Operator: setting.Conf.UpYunConfig.Operator,
		Password: setting.Conf.UpYunConfig.Password,
	})
	_, err = up.Usage()
	if err != nil {
		zap.S().Info("又拍云初始化失败：", err)
	}
	return
}

// UploadAvatar 上传头像
func UploadAvatar(fileName string) (err error) {
	err = up.Put(&upyun.PutObjectConfig{
		Path:      fmt.Sprintf("/img/avatar/%s", fileName),
		LocalPath: fmt.Sprintf("./uploads/avatar/%s", fileName),
	})
	if err != nil {
		zap.S().Info("又拍云上传失败：", err)
	}
	return
}
